package com.purplesoft.zettl.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.purplesoft.zettl.DatabaseTables.TblCart;
import com.purplesoft.zettl.DatabaseTables.TblCategories;
import com.purplesoft.zettl.DatabaseTables.TblGroceries;
import com.purplesoft.zettl.DatabaseTables.TblItems;
import com.purplesoft.zettl.DatabaseTables.TblLists;
import com.purplesoft.zettl.DatabaseTables.TblManufacturer;
import com.purplesoft.zettl.DatabaseTables.TblMeasures;
import com.purplesoft.zettl.DatabaseTables.TblPrices;
import com.purplesoft.zettl.DatabaseTables.TblPurchase;
import com.purplesoft.zettl.DatabaseViews.VwItems;
import com.purplesoft.zettl.DatabaseViews.VwPurchaseItems;
import com.purplesoft.zettl.Utilities.CurrentDateTime;

public class DatabaseOpenHelper extends SQLiteOpenHelper {

	// DatabaseOpenHelper constructor
	public DatabaseOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// create table "tblItems" in database
		// "zettl.db"
		db.execSQL(TblItems.createTable);
		// create table "tblManufacturer" in database
		// "zettl.db"
		db.execSQL(TblManufacturer.createTable);
		// create table "tblCategories" in database
		// "zettl.db"
		db.execSQL(TblCategories.createTable);
		// create table "tblGroceries" in database
		// "zettl.db"
		db.execSQL(TblGroceries.createTable);
		// create table "tblMeasures" in database
		// "zettl.db"
		db.execSQL(TblMeasures.createTable);
		// create table "tblPrices" in database
		// "zettl.db"
		db.execSQL(TblPrices.createTable);
		// create table "tblPurchase" in database
		// "zettl.db"
		db.execSQL(TblPurchase.createTable);
		// create table "tblCart" in database
		// "zettl.db"
		db.execSQL(TblCart.createTable);
		// create table "tblLists" in database
		// "zettl.db"
		db.execSQL(TblLists.createTable);
		// insert samples from createSamples method
		// into table "tblItems" after table was created
		createSamples(db);
		// create database views
		createViews(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	// views after tables was created and populated with sample data
	private void createViews(SQLiteDatabase db) {
		db.execSQL(VwItems.createView);
		db.execSQL(VwPurchaseItems.createView);
	}

	// samples for "tbl_items" after table was created
	private void createSamples(SQLiteDatabase db) {
		// TODO: db.insert Umschreiben auf ContentProvider
		String currentDateTime = new CurrentDateTime().getNow();
		ContentValues contentValues = new ContentValues();
		contentValues.put(TblItems.Columns._ID, -99);
		contentValues.put(TblItems.Columns.Created, currentDateTime);
		contentValues.put(TblItems.Columns.Modified, currentDateTime);
		db.insert(TblPrices.tableName, null, contentValues);
		db.insert(TblCategories.tableName, null, contentValues);
		db.insert(TblMeasures.tableName, null, contentValues);
		db.insert(TblManufacturer.tableName, null, contentValues);
		db.insert(TblGroceries.tableName, null, contentValues);
		db.insert(TblLists.tableName, null, contentValues);
		ContentValues contentValuesItems = new ContentValues();
		ContentValues contentValuesManufacturer = new ContentValues();
		contentValuesManufacturer.clear();
		contentValuesManufacturer.put(TblManufacturer.Columns.Manufacturer,
				"Berchtesgadener Land");
		contentValuesManufacturer.put(TblManufacturer.Columns.Created,
				currentDateTime);
		contentValuesManufacturer.put(TblManufacturer.Columns.Modified,
				currentDateTime);
		db.insert(TblManufacturer.tableName, null, contentValuesManufacturer);
		contentValuesManufacturer.clear();
		contentValuesItems.put(TblItems.Columns.Item, "Milch");
		contentValuesItems.put(TblItems.Columns.ItemDescription,
				"Haltbare Bergbauern Milch fettarm (1,5%)");
		contentValuesItems.put(TblItems.Columns.ManufacturerID, 1);
		contentValuesItems.put(TblItems.Columns.CategoryID, -99);
		contentValuesItems.put(TblItems.Columns.MeasureID, -99);
//		contentValuesItems.put(TblItems.Columns.Price, 0.0);
		contentValuesItems.put(TblItems.Columns.Created, currentDateTime);
		contentValuesItems.put(TblItems.Columns.Modified, currentDateTime);
		db.insert(TblItems.tableName, null, contentValuesItems);
		contentValuesItems.clear();
		contentValuesManufacturer.put(TblManufacturer.Columns.Manufacturer,
				"Barilla");
		contentValuesManufacturer.put(TblManufacturer.Columns.Created,
				currentDateTime);
		contentValuesManufacturer.put(TblManufacturer.Columns.Modified,
				currentDateTime);
		db.insert(TblManufacturer.tableName, null, contentValuesManufacturer);
		contentValuesManufacturer.clear();
		contentValuesItems.put(TblItems.Columns.Item, "Penne");
		contentValuesItems.put(TblItems.Columns.ItemDescription,
				"Penne Rigate No. 73");
		contentValuesItems.put(TblItems.Columns.ManufacturerID, 2);
		contentValuesItems.put(TblItems.Columns.CategoryID, -99);
		contentValuesItems.put(TblItems.Columns.MeasureID, -99);
//		contentValuesItems.put(TblItems.Columns.Price, 0.0);
		contentValuesItems.put(TblItems.Columns.Created, currentDateTime);
		contentValuesItems.put(TblItems.Columns.Modified, currentDateTime);
		db.insert(TblItems.tableName, null, contentValuesItems);
		contentValuesItems.clear();
		contentValuesManufacturer.put(TblManufacturer.Columns.Manufacturer,
				"Danone");
		contentValuesManufacturer.put(TblManufacturer.Columns.Created,
				currentDateTime);
		contentValuesManufacturer.put(TblManufacturer.Columns.Modified,
				currentDateTime);
		db.insert(TblManufacturer.tableName, null, contentValuesManufacturer);
		contentValuesManufacturer.clear();
		contentValuesItems.put(TblItems.Columns.Item, "Natur Joghurt");
		contentValuesItems.put(TblItems.Columns.ItemDescription,
				"Activia Joghurt Natur");
		contentValuesItems.put(TblItems.Columns.ManufacturerID, 3);
		contentValuesItems.put(TblItems.Columns.CategoryID, -99);
		contentValuesItems.put(TblItems.Columns.MeasureID, -99);
//		contentValuesItems.put(TblItems.Columns.Price, 0.0);
		contentValuesItems.put(TblItems.Columns.Created, currentDateTime);
		contentValuesItems.put(TblItems.Columns.Modified, currentDateTime);
		db.insert(TblItems.tableName, null, contentValuesItems);
		contentValuesItems.clear();
		contentValuesManufacturer.put(TblManufacturer.Columns.Manufacturer,
				"Dr. Oetker");
		contentValuesManufacturer.put(TblManufacturer.Columns.Created,
				currentDateTime);
		contentValuesManufacturer.put(TblManufacturer.Columns.Modified,
				currentDateTime);
		db.insert(TblManufacturer.tableName, null, contentValuesManufacturer);
		contentValuesManufacturer.clear();
		contentValuesItems.put(TblItems.Columns.Item, "Pizza Salami");
		contentValuesItems.put(TblItems.Columns.ItemDescription,
				"Steinofen Tradizionale Salame");
		contentValuesItems.put(TblItems.Columns.ManufacturerID, 4);
		contentValuesItems.put(TblItems.Columns.CategoryID, -99);
		contentValuesItems.put(TblItems.Columns.MeasureID, -99);
//		contentValuesItems.put(TblItems.Columns.Price, 0.0);
		contentValuesItems.put(TblItems.Columns.Created, currentDateTime);
		contentValuesItems.put(TblItems.Columns.Modified, currentDateTime);
		db.insert(TblItems.tableName, null, contentValuesItems);
		contentValuesItems.clear();
	}
}
